﻿using System;
using System.Web;
using System.Runtime.Caching;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Ruandao.HostServer
{
    using Ruandao.Misc;

    [DataContract]
    abstract public class Site
    {
        [DataMember]
        public Guid SiteGuid { get; set; }
        [DataMember]
        public string SiteName { get; set; }
        [DataMember]
        public string Title { get; set; }
        [DataMember]
        public string Description { get; set; }
        [DataMember]
        public Guid OwnerGuid { get; set; }
        [DataMember]
        public string UserLevel { get; set; }
        [DataMember]
        public DateTime CreateDate { get; set; }
        [DataMember]
        public int DatabaseId { get; set; }
        [DataMember]
        public string SiteDbName { get; set; }
        [DataMember]
        public int FileStorageId { get; set; }
        [DataMember]
        public bool ContainsAccount { get; set; }

        virtual public Database GetDatabase()
        {
            return HostServerContext.GetInstance().GetAllDatabases()[this.DatabaseId];
        }

        virtual public FileStorage GetFileStorage()
        {
            return HostServerContext.GetInstance().GetAllFileStorages()[this.FileStorageId];
        }

        virtual public string GetFileLocation()
        {
            return GetFileStorage().Location;
        }

        virtual public string GetDbConnectionString()
        {
            var db = GetDatabase();
            return string.Format(db.Connection, db.Instance, this.SiteDbName);
        }

        public Domain GetDomainByType(string type)
        {
            if (!GetDomains().ContainsKey(type)) return null;
            string name = GetDomains()[type][0];
            return HostServerContext.GetInstance().GetAllDomains()[name];
        }

        // 返回各个网站的所有域名字典，key是DomainType, value是DomainName。
        // 性能：根据DomainNames可以进一步从缓存中直接得到DomainObject。
        abstract public IDictionary<string, List<string>> GetDomains();

    }

}
